﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MinistryStandardPlatformBusiness.Helper
{
    public class Log
    {
        private static System.Timers.Timer autoDeleteTimer;
        public static void WriteLog(string fileLogName, string sMsg)
        {
            if (sMsg != "" && fileLogName != "")
            {
                //Random randObj = new Random(DateTime.Now.Millisecond);
                //int file = randObj.Next() + 1;
                string filename = DateTime.Now.ToString("yyyyMMddHH") + fileLogName + ".log";
                try
                {
                    FileInfo fi = new FileInfo(
                        MinistryStandardPlatformBusiness.MessagePool.QueueSession.UserSession.LogPath
                        + filename);

                    if (!fi.Exists)
                    {
                        using (StreamWriter sw = fi.CreateText())
                        {
                            sw.WriteLine(DateTime.Now + "\n" + sMsg + "\n");
                            sw.Close();
                        }
                    }
                    else
                    {
                        using (StreamWriter sw = fi.AppendText())
                        {
                            sw.WriteLine(DateTime.Now + "\n" + sMsg + "\n");
                            sw.Close();
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }

        public static void DeleteLog(object source, System.Timers.ElapsedEventArgs e)
        {
            WriteLog("deletelogerror", "delete start");
            DirectoryInfo dir = new DirectoryInfo(MinistryStandardPlatformBusiness.MessagePool.QueueSession.UserSession.LogPath);
            if (dir == null)
            {
                WriteLog("deletelogerror", "derectory not exists");
                return;
            }
            FileInfo[] files = dir.GetFiles();
            if (files.Length == 0)
            {
                WriteLog("deletelogerror", "no files");
                return;
            }
            for (int i = 0; i < files.Length; i++)
            {
                string date = files[i].Name.Substring(0, 8);
                string oneWeekAgo = DateTime.Now.AddDays(-7).ToString("yyyyMMdd");
                if (string.Compare(date, oneWeekAgo) < 0)
                {
                    try
                    {
                        files[i].Delete();
                    }
                    catch (Exception ex)
                    {
                        WriteLog("deletelogerror", ex.Message);
                        return;
                    }
                }
            }
        }

        static Log()
        {
            ///每天清除7天以前的日志
            autoDeleteTimer = new System.Timers.Timer(60 * 24 * 60 * 1000);//一天
            autoDeleteTimer.Elapsed += new System.Timers.ElapsedEventHandler(DeleteLog);
            autoDeleteTimer.AutoReset = true;//一直执行
            autoDeleteTimer.Enabled = true;//执行事件
            DeleteLog(null, null);
            autoDeleteTimer.Start();
        }
    }
}
